package com.wss.module.main.ui.page;

import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.utils.*;
import com.wss.common.widget.CountClickView;
import com.wss.common.widget.ObserverButton;
import com.wss.module.main.ResourceTable;
import com.wss.module.main.ui.page.slice.CustomViewAbilitySlice;
import com.wss.module.main.ui.view.CircleProgressBar;
import com.wss.module.main.ui.view.FaceScanFloatView;
import com.wss.module.main.ui.view.SwitchButton;
import com.wss.module.main.ui.view.VerificationCodeView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.media.image.PixelMap;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

public class CustomViewAbility extends BaseActionBarAbility
        implements Component.ClickedListener {

    public static final float MAX_STEP = 10000F;

    private VerificationCodeView vcCode;

    private Text tvVcCode;

    private CircleProgressBar cpb01;

    private CircleProgressBar cpb02;

    private SwitchButton switchButton;

    private CountClickView ccvCust;

    private TextField edtName;

    private TextField edtPwd;

    private ObserverButton obBtn;

    private FaceScanFloatView floatView;

    private Image ivLoading;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

//        Window window = getWindow();
//        window.setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);

        findView();
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_custom_view;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void findView() {
        vcCode = (VerificationCodeView) findComponentById(ResourceTable.Id_vc_code);
        tvVcCode = (Text) findComponentById(ResourceTable.Id_tv_vc_code);
        cpb01 = (CircleProgressBar) findComponentById(ResourceTable.Id_cpb_1);
        cpb02 = (CircleProgressBar) findComponentById(ResourceTable.Id_cpb_2);
        switchButton = (SwitchButton) findComponentById(ResourceTable.Id_sb_01);
        ccvCust = (CountClickView) findComponentById(ResourceTable.Id_ccv_cust);
        edtName = (TextField) findComponentById(ResourceTable.Id_edt_name);
        edtPwd = (TextField) findComponentById(ResourceTable.Id_edt_pwd);
        obBtn = (ObserverButton) findComponentById(ResourceTable.Id_ob_btn);
        floatView = (FaceScanFloatView) findComponentById(ResourceTable.Id_circle_view);
        ivLoading = (Image) findComponentById(ResourceTable.Id_iv_loading);

        ScrollView scrollView = (ScrollView) findComponentById(ResourceTable.Id_custom_view_layout);
//        new KeyboardUtils().adjustPan(scrollView);

        Button btnSuccess = (Button) findComponentById(ResourceTable.Id_btn_success);

        Button btnClick = (Button) findComponentById(ResourceTable.Id_btn_click);
        findComponentById(ResourceTable.Id_ob_btn).setClickedListener(this);
        btnSuccess.setClickedListener(this);
        btnSuccess.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
            @Override
            public void onComponentStateChanged(Component component, int i) {

                if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED, i)) {
                    btnSuccess.setBackground(ColorUtils.colorResToElement(getContext(), ResourceTable.Color_orange));
                } else {
                    btnSuccess.setBackground(ColorUtils.colorResToElement(getContext(), ResourceTable.Color_gray));
                }
            }
        });
        btnClick.setClickedListener(this);
        btnClick.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
            @Override
            public void onComponentStateChanged(Component component, int i) {
                if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED, i)) {
                    btnClick.setBackground(ColorUtils.colorResToElement(getContext(), ResourceTable.Color_orange));
                } else {
                    btnClick.setBackground(ColorUtils.colorResToElement(getContext(), ResourceTable.Color_gray));
                }
            }
        });
    }

    private void initView() {
        setCenterText("自定义View展览区");

        vcCode.addOnCodeChangeListener(code -> tvVcCode.setText(String.format("验证码是:%s", code)));
        tvVcCode.setText(String.format("验证码是：%s", vcCode.getText()));

        cpb01.setMaxValue(MAX_STEP);

        switchButton.setOnCheckedChangeListener((view,isChecked) -> {
            ToastUtils.show("选中状态：" + isChecked);
        });

        //自定义背景图
        ccvCust.setButtonRes(ResourceTable.Media_main_stepper_add,
                ResourceTable.Media_main_stepper_add_disable,
                ResourceTable.Media_main_stepper_reduce,
                ResourceTable.Media_input_minus_disabled);
        ccvCust.setBtnParentBg(ResourceTable.Color_white);
        ccvCust.setBtnSize(20, 20);
        ccvCust.setCountViewAttr(ResourceTable.Color_transparent, 0, 4, 4);
        ccvCust.setAfterClickListener(new CountClickView.OnClickAfterListener() {
            @Override
            public void onAfter(CountClickView view, int value) {
                Logger.e("result:" + value);
            }
        });

        obBtn.observer(edtName, edtPwd);

        floatView.startAnimator();
        floatView.setTipsTextColor(ResourceTable.Color_theme);

    }

    public static byte[] getBitmapByte(PixelMap bitmap) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PixelMapUtils.compress(bitmap, "image/jpeg", 100, out);
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_btn_click) {
            //重置环形进度条的数值
            int step = new SecureRandom().nextInt((int) MAX_STEP);
            cpb01.setValue(step);
            float i = (step / MAX_STEP) * 100L;
            cpb02.setValue(i);
        } else if (component.getId() == ResourceTable.Id_btn_success) {
            //设置圆圈浮层 成功View
            floatView.isComplete("认证成功");
            ss();
            getByte();
        } else if (component.getId() == ResourceTable.Id_ob_btn) {
            ToastUtils.show("哈哈哈");
        }
    }

    private void getByte() {
        PixelMap bitmap = PixelMapUtils
                .getPixelMapFromResource(context, ResourceTable.Media_aa);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //参数100表示不压缩
        PixelMapUtils.compress(bitmap, "image/jpeg", 100, bos);
        byte[] bytes = bos.toByteArray();
        Logger.e(Arrays.toString(bytes));
        String s = Base64.encodeToString(bytes, Base64.DEFAULT);
        Logger.e(s);
    }

    private void ss() {
        AnimatorProperty rotation = new AnimatorProperty();
        rotation.setTarget(ivLoading);
        rotation.rotate(-360);
        rotation.setDuration(350);
        rotation.setCurveType(Animator.CurveType.LINEAR);
        rotation.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                ivLoading.setRotation(0);
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
        rotation.setLoopedCount(AnimatorValue.INFINITE);
        rotation.start();
    }
}
